package com.highcom.controller;

import com.highcom.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    // http://localhost/user/requestParam1?name=lilei&age=15
    //方法传递普通类型参数  数量任意  类型必须匹配
    @RequestMapping("/requestParam1")
    public String requestParam1test1(String name,int age){
        System.out.println(name +" " + age);
        //  /user/page.jsp
        return "page.jsp";
    }
    // http://localhost/user/requestParam2?userName=jock


    //@RequestParam获取普通参数
    @RequestMapping("/requestParam2")
    public String requestParam1test1(@RequestParam(value = "userName",required = false) String name){
        System.out.println(name);
        //  /user/page.jsp
        return "page.jsp";
    }

    //方法传递POJO类型参数，url地址中的参数作为POJO的属性直接传入对象
// http://localhost/user/requestParam3?name=lilei&age=15
    @RequestMapping("/requestParam3")
    public String requestParam3test(User user){
        System.out.println(user);
        //  /user/page.jsp
        return "page.jsp";
    }

    // http://localhost/user/requestParam4?name=lilei&age=15
    @RequestMapping("/requestParam4")
    public String requestParam4test(User user,int age){
        System.out.println(user);
        System.out.println(age);
        //  /user/page.jsp
        return "page.jsp";
    }
    //使用对象属性名.属性名的层次结构可以为POJO中的类型参数赋值
    // http://localhost/user/requestParam5?address.city=beijing
    @RequestMapping("/requestParam5")
    public String requestParam5test(User user){
        System.out.println(user.getAddress().getCity());
        //  /user/page.jsp
        return "page.jsp";
    }

    // http://localhost/user/requestParam6?nick=jock&nick=azhen
    @RequestMapping("/requestParam6")
    public String requestParam6test(User user){
        System.out.println(user);
        //  /user/page.jsp
        return "page.jsp";
    }
    // http://localhost/user/requestParam7?addresses[0].city=suzhou&addresses[1].city=yangzhou
    @RequestMapping("/requestParam7")
    public String requestParam7test(User user){
        System.out.println(user.getAddresses());
        //  /user/page.jsp
        return "page.jsp";
    }
    // http://localhost/user/requestParam8?addressMap['job'].city=suzhou&addressMap['home'].city=zhangzhou
    @RequestMapping("/requestParam8")
    public String requestParam8test(User user){
        System.out.println(user.getAddressMap());
        //  /user/page.jsp
        return "page.jsp";
    }
    // http://localhost/user/requestParam9?nick=jock&nick=azhen
    @RequestMapping("/requestParam9")
    public String requestParam9test(String[] nick){
        System.out.println(nick[0]);
        System.out.println(nick[1]);
        //  /user/page.jsp
        return "page.jsp";
    }

    //
    // http://localhost/user/requestParam10?nick=jock&nick=azhen
    @RequestMapping("/requestParam10")
    //public String requestParam10test(List<String> nick){  传递集合类型的普通参数时，无法为nick复制
    public String requestParam10test(@RequestParam("nick") List<String> nick){
        System.out.println(nick);
        //  /user/page.jsp
        return "page.jsp";
    }


    //格式转换需要开启  <mvc:annotation-driven/>
    // http://localhost/user/requestParam11?date=1999-09-09
    @RequestMapping("/requestParam11")
    public String requestParam11test(Date date){
        System.out.println(date);
        //  /user/page.jsp
        return "page.jsp";
    }
    //格式转换需要开启  <mvc:annotation-driven/>
    // http://localhost/user/requestParam12?date=1999-09-09
    @RequestMapping("/requestParam12")
    public String requestParam12test(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        System.out.println(date);
        //  /user/page.jsp
        return "page.jsp";
    }
}
